package com.scut.online_black_market.controller;

import com.scut.online_black_market.dto.ImageUrlResponse;
import com.scut.online_black_market.exception.FileUploadException;
import com.scut.online_black_market.utility.ImageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

/**
 * create by yellowBroke on 2020年10月15日 00点41分
 */

@Api(value = "文件上传接口",tags = "文件上传接口")
@RestController
@RequestMapping(value = "/upload")
public class FileUploadController {

    @Resource
    private ImageUtil imageUtil;

    @ApiOperation(value = "图片上传")
    @RequestMapping(value = "/image", method = RequestMethod.POST, consumes = "multipart/*", headers = "content-type=multipart/form-date")
    public ResponseEntity<ImageUrlResponse> uploadImage(MultipartFile image) throws InterruptedException {

        try {
            String url = imageUtil.saveImage(image).get();
            return ResponseEntity.ok(new ImageUrlResponse(true, url, "上传成功"));
        } catch (FileUploadException | ExecutionException e) {
            return ResponseEntity.ok(new ImageUrlResponse(false, "", e.getMessage()));
        }

    }
}
